package com.dfrz.utils;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.models.*;
import com.aliyun.sdk.service.dysmsapi20170525.*;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;

import java.util.*;
import java.util.concurrent.CompletableFuture;

/**
 * 阿里云短信服务工具类
 * 功能描述，支持发送各种类型的参数
 * @param //map
 * phoneNumber 手机号      signName 签名     templateCode 模板    templateParam 模板参数
 *
 */
public class MessageUtil {
    private static final String REGIONID="cn-hangzhou";
    private static final String DOMAIN="dysmsapi.aliyuncs.com";
    private static final String accessKeyId="LTAI5t5dF138zpM8Zccpeg1v";
    private static final String secret="rIF5aupnE0UDFgeToWN86pY9BZbp1V";

    public static Boolean sendSms(Map<String,String> map) throws Exception {
        boolean flag = false;
        if(map.isEmpty()){
            return flag;
        }
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(accessKeyId)
                .accessKeySecret(secret)
                .build());

        AsyncClient client = AsyncClient.builder()
                .region(REGIONID)
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride(DOMAIN)
                )
                .build();

        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .phoneNumbers(map.get("phoneNumber"))
                .signName(map.get("signName"))
                .templateCode(map.get("templateCode"))
                .templateParam("{\"code\":\""+map.get("templateParam")+"\"}")
                .build();

        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
        SendSmsResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));
        flag = true;
        client.close();
        return flag;
    }
}
